package com.label.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.label.common.CommonResult;
import com.label.entity.Labels;
import com.label.entity.Log;
import com.label.entity.Patientlabels;
import com.label.entity.User;
import com.label.service.LabelsService;
import com.label.service.LogService;
import com.label.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jianhua
 * @since 2022-03-27
 */

@Api
@RestController
@RequestMapping("/api/labels")
public class LabelsController {
    @Autowired
    LabelsService labelsService;
    @Autowired
    LogService logService;
    //    标签列表
    @RequestMapping("/list")
    @ApiOperation("全部标签列表")
    CommonResult list(){
        return CommonResult.success(labelsService.labelList());

    }
    @RequestMapping("/getlist")
    @ApiOperation("标签列表查询page")
    CommonResult list(ModelMap modelMap, Page page2, @ModelAttribute("labels") Labels labels,Integer page,Integer pageSize){
        page2.setCurrent(page);
        page2.setSize(pageSize);
        page2 = labelsService.pageList(page2,labels);

        modelMap.addAttribute("page",page2);
        return CommonResult.success(page2);
    }
//    新建标签保存
    @RequestMapping("/save")
    @ApiOperation("添加标签")
    CommonResult save(Labels labels){
        labels.setLabelstate(1);
        return CommonResult.success(labelsService.saveOrUpdate(labels));
    }
    @RequestMapping("update")
    @ApiOperation("标签修改")
    CommonResult update(Labels labels){
        return CommonResult.success(labelsService.update(labels));
    }
    //id查询
    @RequestMapping("/getByid")
    @ApiOperation("根据id查询标签")
    CommonResult getByid(Integer id){
        System.out.println();
        return CommonResult.success(labelsService.getById(id));
    }
    @RequestMapping("/getBytype")
    @ApiOperation("根据类型获取标签")
    CommonResult getBytype(String type){
        return CommonResult.success(labelsService.getBytype(type));
    }
    //逻辑删除
    @RequestMapping("/del")
    @ApiOperation("标签删除")
    CommonResult del(HttpSession session,Integer id){
        Labels labels=labelsService.getById(id);
        labels.setLabelstate(0);
            return CommonResult.success(labelsService.updateById(labels));
    }
    //批量删除
    @RequestMapping("/delByIds")
    CommonResult delByIds(Integer[] id){

        return CommonResult.success(labelsService.removeByIds(Arrays.asList(id)));
    }
//    模糊查询
    @RequestMapping("/getlike")
    @ApiOperation("标签模糊查询")
    CommonResult getlistBylikes(ModelMap modelMap, Page page2,Integer page,Integer pageSize,@ModelAttribute("key") String key){
        page2.setCurrent(page);
        page2.setSize(pageSize);
        page2 = labelsService.getBykey(page2,key);

        modelMap.addAttribute("page",page2);
        return CommonResult.success(page2);
       }
}
